#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<iostream>
using namespace std;

class String
{
public:
    String(const char *src)
    {
        if(src!=NULL)
        {
            int length=strlen(src);
            m_data=new char[length+1];
            strcpy(m_data,src);
        }
        cout<<"in String(char *)"<<endl;

        cout<<m_data<<endl;
    }

    String()
    {
        cout<<"in String();"<<endl;
        cout<<m_data<<endl;
    }

    String(const String& src)
    {
        if(src.m_data!=NULL)
        {
            int length=strlen(src.m_data);
            m_data=new char[length+1];
            strcpy(m_data,src.m_data);
        }

        cout<<"in String(String&)"<<endl;

        cout<<m_data<<endl;
    }


    String& operator=(const String& src)
    {
        if(src.m_data!=NULL)
        {
            if(m_data==NULL || strlen(m_data)<strlen(src.m_data))
            {
                char *temp=m_data;
                delete[] temp;
            }
            int length=strlen(src.m_data);
            m_data=new char[length+1];
            strcpy(m_data,src.m_data);
        }

        cout<<"in =(String&)"<<endl;

        cout<<m_data<<endl;

        return *this;
    }
    ~String()
    {
        if(m_data!=NULL)
            delete[] m_data;
        cout<<"in ~String"<<endl;
    }
private:
    char *m_data;
};

int main()
{
    //char *src="nihao";
    char src[6]={'n','i','h','a','o','\0'};

    cout<<"String s1(src)"<<endl;
    String s1(src);
    cout<<"String s2(s1)"<<endl;
    String s2(s1);
    cout<<"String s3;"<<endl;
    String s3;
    cout<<"s3=s1"<<endl;
    s3=s1;

    printf("%d\n",0|2);
}
